package com.demo.ping.service.impl;

import com.demo.ping.dao.ErrorLogsRepository;
import com.demo.ping.orm.entity.ErrorLogs;
import com.demo.ping.service.ErrorLogsService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import java.time.OffsetDateTime;

/**
 *  @author Jofo
 */
@Service
public class ErrorLogsServiceImpl implements ErrorLogsService {
    @Resource
    private ErrorLogsRepository errorLogsRepository;

    @Value("${spring.application.name}")
    private String serviceName;

    @Override
    public void saveErrorLog(ErrorLogs errorLogs) {
        Assert.notNull(errorLogs.getErrorCode(), "errorCode is required");
        Assert.notNull(errorLogs.getErrorMessage(), "errorMessage is required");
        Assert.notNull(errorLogs.getErrorLevel(), "errorLevel is required");
        errorLogs.setCreateTime(OffsetDateTime.now());
        errorLogs.setServiceName(serviceName);
        errorLogsRepository.save(errorLogs);
    }
}
